﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChineseSchool.Models.Hethong;
using ChineseSchool.Models;

namespace ChineseSchool.BLL
{
    public class B_Lessons
    {
        private PetaPoco.Database db;
        public B_Lessons()
        {
            db = new PetaPoco.Database("ConnectionString");
        }
        
        public List<M_Lessons> GetAlls()
        {
            var Lessons = db.Fetch<M_Lessons>(@";EXEC Lessons_Get_List");
            return Lessons;
        }

        public M_Lessons GetByPK(int LessonsID)
        {
            var loadLessons = db.SingleOrDefault<M_Lessons>("SELECT * FROM Lessons m WHERE m.LessonID = " + LessonsID + ";");
            db.CloseSharedConnection();
            return loadLessons;
        }

        public List<M_Lessons> GetBy_LessonsParentID(int LessonsParentID)
        {
            var Lessons = db.Fetch<M_Lessons>(@";EXEC Lessons_GetByLessonsParentId @@LessonsParentID = @0", LessonsParentID);
            return Lessons;
        }

        public List<M_Lessons> GetBy_LessonsParentID(int? LessonsParentID)
        {
            var Lessons = new List<M_Lessons>();
            if (LessonsParentID.HasValue)
                Lessons = db.Fetch<M_Lessons>(@"SELECT * FROM Lessons WHERE LessonID = '" + LessonsParentID + "'");
            else
                Lessons = db.Fetch<M_Lessons>(@"SELECT * FROM Lessons WHERE LessonID = '0'");
            return Lessons;
        }

        public List<M_Lessons> GetBy_MenuID(int MenuID)
        {
            var Lessons = db.Fetch<M_Lessons>(@";EXEC Lessons_GetByMenuId @@MenuId = @0", MenuID);
            return Lessons;
        }

        public List<M_Lessons> GetLastestLesson()
        {
            var strSql = @"SELECT TOP 10 LessonID, LessonTitle, PostDate
                            FROM Lessons
                            ORDER BY PostDate DESC";
            var lstTopUser = db.Fetch<M_Lessons>(strSql);
            return lstTopUser;
        }

        public List<M_Lessons> GetTop3Lesson()
        {
            var strSql = @"SELECT TOP 3 *
                            FROM Lessons
                            ORDER BY PostDate DESC";
            var lstTopUser = db.Fetch<M_Lessons>(strSql);
            return lstTopUser;
        }

        public ObjectWithPaging<M_Lessons> GetAllWithPaging(int pageIndex, int pageSize)
        {
            var result = db.Page<M_Lessons>(pageIndex, pageSize, "SELECT * FROM Lessons");

            ObjectWithPaging<M_Lessons> activities = new ObjectWithPaging<M_Lessons>();

            activities.CurrentPage = result.CurrentPage;

            activities.TotalPages = result.TotalPages;
            activities.TotalItems = result.TotalItems;
            activities.Items = result.Items;

            return activities;
        }
    }
}